# coding: utf-8
from sqlalchemy import BigInteger, Column, DateTime, Integer, String
from sqlalchemy.schema import FetchedValue
from flask_sqlalchemy import SQLAlchemy
from common.models.Shop import Shop
from common.models.TableItem import TableItem
from common.models.Customer import Customer
from common.models.Food import Food, FoodType

from application import db

class Order(db.Model):
    __tablename__ = 'order'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    order_sn = db.Column(db.String(40), nullable=False, unique=True, server_default=db.FetchedValue())
    meal_number = db.Column(db.String(10), nullable=False )
    c_id = db.Column(db.ForeignKey('food_db.customer.id'), nullable=False, index=True)
    total_price = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())
    note = db.Column(db.Text, nullable=False)
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    shop_id = db.Column(db.ForeignKey('food_db.shop.id'), nullable=False, index=True)
    status = db.Column(db.Integer, nullable=False)
    table_id = db.Column(db.ForeignKey('food_db.table_item.id'), nullable=False, index=True)
    minus_cash = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())

    order_item = db.relationship("OrderItem", backref="parent")
    c = db.relationship('Customer', primaryjoin='Order.c_id == Customer.id', backref='orders')
    shop = db.relationship('Shop', primaryjoin='Order.shop_id == Shop.id', backref='orders')
    table = db.relationship('TableItem', primaryjoin='Order.table_id == TableItem.id', backref='orders')

class OrderItem(db.Model):
    __tablename__ = 'order_item'
    __table_args__ = {'schema': 'food_db'}

    id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.ForeignKey('food_db.order.id'), nullable=False, index=True)
    count = db.Column(db.Integer, nullable=False, server_default=db.FetchedValue())
    price = db.Column(db.Numeric(10, 2), nullable=False, server_default=db.FetchedValue())
    food_id = db.Column(db.ForeignKey('food_db.food.id'), nullable=False, index=True)
    updated_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())
    created_time = db.Column(db.DateTime, nullable=False, server_default=db.FetchedValue())

    food = db.relationship('Food', primaryjoin='OrderItem.food_id == Food.id', backref='food')
    order = db.relationship('Order', primaryjoin='OrderItem.order_id == Order.id', backref='order')
